package generic.net;

/**
 * A request for a response.
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class Request extends Message {
    private static int nextRequestID = 0;
    private int requestID;
    private String serviceProvider;
    private String requestType;
    private Object[] payload;

    /**
     * Create a new request with paramaters.
     * @param serviceProvider   The name of the service provider
     * @param requestType       The request type
     * @param payload           the arguments passed to the service provider
     */
    public Request(String serviceProvider, String requestType, Object ... payload) {
        init();
        this.requestType = requestType;
        this.serviceProvider = serviceProvider;
        this.payload = payload;
    }

    /**
     * Create a new request.
     * @param serviceProvider   The name of the service provider
     * @param requestType       The request type
     */
    public Request(String serviceProvider, String requestType) {
        init();
        this.requestType = requestType;
        this.serviceProvider = serviceProvider;
    }

    /**
     * Default contstructor should only be used by XMLEncoder.
     */
    public Request() {
        type = "REQUEST";
    }

    /**
     * Generic init.
     */
    private void init() {
        type = "REQUEST";
        requestID = nextRequestID;
        nextRequestID++;
    }

    /**
     * Every request/response pair has a unique identifier (between each set of peers).
     *
     * @return the unique identifier of this request.
     */
    public int getID() {
        return requestID;
    }

    /**
     * Sets the unique identifier used by a request/response pair. As you can
     * Imagine this is a bad idea 99% of the time. Here for XMLEncoder.
     * @param requestID
     */
    public void setID(int requestID) {
        this.requestID = requestID;
    }

    /**
     * Returns the name of the service provider this request is destined for.
     * @return the name of the service provider this request is destined for.
     */
    public String getServiceProvider() {
        return serviceProvider;
    }

    /**
     * Set the name of the service provider this request is destined for.
     * @param serviceProvider   The name of a service provider.
     */
    public void setServiceProvider(String serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    /**
     * Every request has a 'payload' which essentially becomes zero or more arguments
     * to the request.
     *
     * @return  The requests payload.
     */
    public Object[] getPayload() {
        return payload;
    }

    /**
     * Every request has a 'payload' which essentially becomes zero or more arguments
     * to the request.
     *
     * @param payload   The new payload. variable lenth argument list here.
     */
    public void setPayload(Object[] payload) {
        this.payload = payload;
    }

    /**
     * Request types are used to differentiate reqests to the same service.
     * Using the Class wrapper this becomes method calls.
     *
     * @return The request type.
     */
    public String getRequestType() {
        return requestType;
    }

    /**
     * Request types are used to differentiate reqests to the same service.
     * Using the Class wrapper this becomes method calls.
     *
     * @param requestType The request type for this request.
     */
    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }


}
